package dproxy.application.ui.commands;

import java.util.Collection;
import java.util.Iterator;

import dproxy.application.ui.CommandLineProcessor;
import dproxy.server.cache.bo.CacheBO;
import dproxy.server.cache.dao.DatabaseImplentation;
import dproxy.server.cache.to.RequestResponse;
import dproxy.server.common.util.UtilConvert;

/**
 * Shows details about a cache entry.
 */
public class Detail implements Command {

    /** Sub-Command that shows contents of request. */
    private static final String SC_REQUEST = "request";
    /** Sub-Command that shows contents of response. */
    private static final String SC_RESPONSE = "response";
    /** Sub-Command that shows contents of request key. */
    private static final String SC_KEY = "key";
        
    /** Sub command (command option). */
    private String subCommand;
    /** Number numeric parameter. */
    private int firstNumberParameter;

    /**
     * @see dproxy.application.ui.commands.Command#description()
     */
    public String description() {
        return 
            "   'detail'     - Show contents of an entry\n" +
            "                  - Options:\n" +
            "                    " + SC_REQUEST + " N    - shows contents of request of entry N\n" +
            "                    " + SC_RESPONSE + " N   - shows contents of response of entry N\n" +
            "                    " + SC_KEY + " N        - shows contents of key of entry N";
    }

    /**
     * @see dproxy.application.ui.commands.Command#validate(Collection)
     */
    public void validate(Collection<String> parameters) throws ValidationException {
        
        Iterator <String> itParameters = parameters.iterator();
        if (itParameters.hasNext()) {
            subCommand = itParameters.next();
            if (SC_REQUEST.equals(subCommand)
                 || SC_RESPONSE.equals(subCommand)
                 || SC_KEY.equals(subCommand)) {
                firstNumberParameter = ValidationUtils.getNumberParameter(itParameters);
                ValidationUtils.validateNoMoreParameters(itParameters);
            } else {
                throw new ValidationException();
            }
        } else {
            throw new ValidationException();
        }
        
    }

    /**
     * @see dproxy.application.ui.commands.Command#confirm()
     */
    public boolean confirm() {
        // Detail operation needs no confirmation
        return true;
    }

    /**
     * @see dproxy.application.ui.commands.Command#execute()
     */
    public void execute() {
        DatabaseImplentation cacheImplementation
            = CommandLineProcessor.getInstance().getCacheImplementation();
        String contents = null;
        RequestResponse rr = CacheBO.getInstance(cacheImplementation).findByIndex(firstNumberParameter);
        if (rr != null) {
            if (SC_REQUEST.equals(subCommand)) {
                contents = UtilConvert.convertToString(rr.getRequestContents());
            } else if (SC_RESPONSE.equals(subCommand)) {
                contents = UtilConvert.convertToString(rr.getResponseContents());
            } else if (SC_KEY.equals(subCommand)) {
                contents = rr.getKeyNumber() + "-" + rr.getKey();
            }
            CommandLineProcessor.getInstance().write(
                    "<[" + contents + "]>");
        } else {
            CommandLineProcessor.getInstance().write(
                    "Entry not found");
        }
    }

}
